package org.jivesoftware.smack.packet;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.CopyOnWriteArrayList;
import org.jivesoftware.smack.util.StringUtils;

public abstract class Packet
{
  protected static final String DEFAULT_LANGUAGE = Locale.getDefault().getLanguage().toLowerCase();
  private static String DEFAULT_XML_NS = null;
  public static final String ID_NOT_AVAILABLE = "ID_NOT_AVAILABLE";
  public static final DateFormat XEP_0082_UTC_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  private static String prefix = StringUtils.randomString(5) + "-";
  private static long id = 0L;
  private String xmlns = DEFAULT_XML_NS;
  private String packetID = null;
  private String to = null;
  private String from = null;
  private final List<PacketExtension> packetExtensions = new CopyOnWriteArrayList();
  private final Map<String, Object> properties = new HashMap();
  private XMPPError error = null;

  static
  {
    XEP_0082_UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
  }

  public static synchronized String nextID()
  {
    return prefix + Long.toString(id++);
  }

  public static void setDefaultXmlns(String paramString)
  {
    DEFAULT_XML_NS = paramString;
  }

  public Packet()
  {
  }

  public Packet(Packet paramPacket)
  {
    this.packetID = paramPacket.getPacketID();
    this.to = paramPacket.getTo();
    this.from = paramPacket.getFrom();
    this.xmlns = paramPacket.xmlns;
    this.error = paramPacket.error;
    Iterator localIterator = paramPacket.getExtensions().iterator();
    while (localIterator.hasNext())
    {
      PacketExtension localPacketExtension = (PacketExtension)localIterator.next();
      addExtension(localPacketExtension);
    }
  }

  public String getPacketID()
  {
    if ("ID_NOT_AVAILABLE".equals(this.packetID))
      return null;
    if (this.packetID == null)
      this.packetID = nextID();
    return this.packetID;
  }

  public void setPacketID(String paramString)
  {
    this.packetID = paramString;
  }

  public String getTo()
  {
    return this.to;
  }

  public void setTo(String paramString)
  {
    this.to = paramString;
  }

  public String getFrom()
  {
    return this.from;
  }

  public void setFrom(String paramString)
  {
    this.from = paramString;
  }

  public XMPPError getError()
  {
    return this.error;
  }

  public void setError(XMPPError paramXMPPError)
  {
    this.error = paramXMPPError;
  }

  public synchronized Collection<PacketExtension> getExtensions()
  {
    if (this.packetExtensions == null)
      return Collections.emptyList();
    return Collections.unmodifiableList(new ArrayList(this.packetExtensions));
  }

  public PacketExtension getExtension(String paramString)
  {
    return getExtension(null, paramString);
  }

  public PacketExtension getExtension(String paramString1, String paramString2)
  {
    if (paramString2 == null)
      return null;
    Iterator localIterator = this.packetExtensions.iterator();
    while (localIterator.hasNext())
    {
      PacketExtension localPacketExtension = (PacketExtension)localIterator.next();
      if (((paramString1 == null) || (paramString1.equals(localPacketExtension.getElementName()))) && (paramString2.equals(localPacketExtension.getNamespace())))
        return localPacketExtension;
    }
    return null;
  }

  public void addExtension(PacketExtension paramPacketExtension)
  {
    if (paramPacketExtension == null)
      return;
    this.packetExtensions.add(paramPacketExtension);
  }

  public void addExtensions(Collection<PacketExtension> paramCollection)
  {
    if (paramCollection == null)
      return;
    this.packetExtensions.addAll(paramCollection);
  }

  public void removeExtension(PacketExtension paramPacketExtension)
  {
    this.packetExtensions.remove(paramPacketExtension);
  }

  public synchronized Object getProperty(String paramString)
  {
    if (this.properties == null)
      return null;
    return this.properties.get(paramString);
  }

  public synchronized void setProperty(String paramString, Object paramObject)
  {
    if (!(paramObject instanceof Serializable))
      throw new IllegalArgumentException("Value must be serialiazble");
    this.properties.put(paramString, paramObject);
  }

  public synchronized void deleteProperty(String paramString)
  {
    if (this.properties == null)
      return;
    this.properties.remove(paramString);
  }

  public synchronized Collection<String> getPropertyNames()
  {
    if (this.properties == null)
      return Collections.emptySet();
    return Collections.unmodifiableSet(new HashSet(this.properties.keySet()));
  }

  public abstract String toXML();

  protected synchronized String getExtensionsXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    Iterator localIterator = getExtensions().iterator();
    Object localObject1;
    while (localIterator.hasNext())
    {
      localObject1 = (PacketExtension)localIterator.next();
      localStringBuilder.append(((PacketExtension)localObject1).toXML());
    }
    if ((this.properties != null) && (!this.properties.isEmpty()))
    {
      localStringBuilder.append("<properties xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">");
      localIterator = getPropertyNames().iterator();
      while (localIterator.hasNext())
      {
        localObject1 = (String)localIterator.next();
        Object localObject2 = getProperty((String)localObject1);
        localStringBuilder.append("<property>");
        localStringBuilder.append("<name>").append(StringUtils.escapeForXML((String)localObject1)).append("</name>");
        localStringBuilder.append("<value type=\"");
        if ((localObject2 instanceof Integer))
        {
          localStringBuilder.append("integer\">").append(localObject2).append("</value>");
        }
        else if ((localObject2 instanceof Long))
        {
          localStringBuilder.append("long\">").append(localObject2).append("</value>");
        }
        else if ((localObject2 instanceof Float))
        {
          localStringBuilder.append("float\">").append(localObject2).append("</value>");
        }
        else if ((localObject2 instanceof Double))
        {
          localStringBuilder.append("double\">").append(localObject2).append("</value>");
        }
        else if ((localObject2 instanceof Boolean))
        {
          localStringBuilder.append("boolean\">").append(localObject2).append("</value>");
        }
        else if ((localObject2 instanceof String))
        {
          localStringBuilder.append("string\">");
          localStringBuilder.append(StringUtils.escapeForXML((String)localObject2));
          localStringBuilder.append("</value>");
        }
        else
        {
          ByteArrayOutputStream localByteArrayOutputStream = null;
          ObjectOutputStream localObjectOutputStream = null;
          try
          {
            localByteArrayOutputStream = new ByteArrayOutputStream();
            localObjectOutputStream = new ObjectOutputStream(localByteArrayOutputStream);
            localObjectOutputStream.writeObject(localObject2);
            localStringBuilder.append("java-object\">");
            String str = StringUtils.encodeBase64(localByteArrayOutputStream.toByteArray());
            localStringBuilder.append(str).append("</value>");
          }
          catch (Exception localException1)
          {
            localException1.printStackTrace();
            if (localObjectOutputStream != null)
              try
              {
                localObjectOutputStream.close();
              }
              catch (Exception localException2)
              {
              }
            if (localByteArrayOutputStream != null)
              try
              {
                localByteArrayOutputStream.close();
              }
              catch (Exception localException3)
              {
              }
          }
          finally
          {
            if (localObjectOutputStream != null)
              try
              {
                localObjectOutputStream.close();
              }
              catch (Exception localException4)
              {
              }
            if (localByteArrayOutputStream != null)
              try
              {
                localByteArrayOutputStream.close();
              }
              catch (Exception localException5)
              {
              }
          }
        }
        localStringBuilder.append("</property>");
      }
      localStringBuilder.append("</properties>");
    }
    return localStringBuilder.toString();
  }

  public String getXmlns()
  {
    return this.xmlns;
  }

  public static String getDefaultLanguage()
  {
    return DEFAULT_LANGUAGE;
  }

  public boolean equals(Object paramObject)
  {
    if (this == paramObject)
      return true;
    if ((paramObject == null) || (getClass() != paramObject.getClass()))
      return false;
    Packet localPacket = (Packet)paramObject;
    if (this.error != null ? !this.error.equals(localPacket.error) : localPacket.error != null)
      return false;
    if (this.from != null ? !this.from.equals(localPacket.from) : localPacket.from != null)
      return false;
    if (!this.packetExtensions.equals(localPacket.packetExtensions))
      return false;
    if (this.packetID != null ? !this.packetID.equals(localPacket.packetID) : localPacket.packetID != null)
      return false;
    if (this.properties != null ? !this.properties.equals(localPacket.properties) : localPacket.properties != null)
      return false;
    if (this.to != null ? !this.to.equals(localPacket.to) : localPacket.to != null)
      return false;
    return this.xmlns != null ? this.xmlns.equals(localPacket.xmlns) : localPacket.xmlns == null;
  }

  public int hashCode()
  {
    int i = this.xmlns != null ? this.xmlns.hashCode() : 0;
    i = 31 * i + (this.packetID != null ? this.packetID.hashCode() : 0);
    i = 31 * i + (this.to != null ? this.to.hashCode() : 0);
    i = 31 * i + (this.from != null ? this.from.hashCode() : 0);
    i = 31 * i + this.packetExtensions.hashCode();
    i = 31 * i + this.properties.hashCode();
    i = 31 * i + (this.error != null ? this.error.hashCode() : 0);
    return i;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.packet.Packet
 * JD-Core Version:    0.6.1
 */